package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 加入购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){
        log.info("添加购物车, shoppingCart={}",shoppingCart.toString());

        //1. 先判断当前加入购物车的 菜品 / 套餐 , 在当前用户的购物车中是否已经存在
        //查询条件 ----> user_id , dish_id/setmeal_id
        LambdaQueryWrapper<ShoppingCart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        if(shoppingCart.getDishId() != null){
            cartLambdaQueryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }else{
            cartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart shoppingCartDB = shoppingCartService.getOne(cartLambdaQueryWrapper);

        //2. 如果不存在 , 组装数据, 直接保存购物车对象
        if(shoppingCartDB == null){
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCartDB = shoppingCart;
        }else{
            //3. 如果存在 , 将其数量 number + 1 , 更新
            shoppingCartDB.setNumber(shoppingCartDB.getNumber()+1);
            shoppingCartService.updateById(shoppingCartDB);
        }

        return R.success(shoppingCartDB);
    }


    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查询用户的购物车列表数据");

        //组装条件
        LambdaQueryWrapper<ShoppingCart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        cartLambdaQueryWrapper.orderByDesc(ShoppingCart::getCreateTime);

        //执行查询
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(cartLambdaQueryWrapper);
        return R.success(shoppingCartList);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R clean(){
        log.info("清空当前用户的购物车数据 .... ");

        //组装条件
        LambdaQueryWrapper<ShoppingCart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        //执行删除
        shoppingCartService.remove(cartLambdaQueryWrapper);
        return R.success();
    }







    //---------------------------------------- 实战功能 -----------------------
    /**
     * 减少购物车数量
     * @param cart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart cart){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        queryWrapper.eq(cart.getDishId() != null, ShoppingCart::getDishId, cart.getDishId());
        queryWrapper.eq(cart.getSetmealId() != null, ShoppingCart::getSetmealId, cart.getSetmealId());

        ShoppingCart one = shoppingCartService.getOne(queryWrapper);

        if(one != null){
            one.setNumber(one.getNumber()-1);
            if(one.getNumber()>0){
                shoppingCartService.updateById(one);
            }else{
                shoppingCartService.remove(queryWrapper);
            }
        }

        return R.success(one);
    }



}
